package com.dwd.org.service.impl;

import com.dwd.basic.service.impl.BaseServiceImpl;
import com.dwd.org.mapper.DepartmentMapper;
import com.dwd.org.pojo.Department;
import com.dwd.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/5 11:29
 * @Version 1.0
 */
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;


    /**
     * 修改一二级部门查询
     *
     * @return
     */
    @Override
    public List<Department> queryparent(Long id) {
        //查询一二级
        List<Department> departments = departmentMapper.queryLevelOneAndTwo(id);
        Long id2 = departmentMapper.queryParentId(id);
        //得到二级
        for (Department department : departments) {
            //当前id
            Long id1 = department.getId();
            //排除同等级和下等级,判断父id和当前id是否相等,不相等继续执行
            if (!id1.equals(id2)) {
                List<Department> children = department.getChildren();
                //如果二级部门没有设置null
                if (children.size() == 0) {
                    department.setChildren(null);
                } else {
                    queryMulitParentDept(children, id);
                }
            } else {
                department.setChildren(null);
            }

        }
        return departments;
    }

    /**
     * 增加一二级部门查询
     *
     * @return
     */
    @Override
    public List<Department> queryparent2() {
        //查询一二级
        List<Department> departments = departmentMapper.queryLevelOneAndTwo2();
        for (Department department : departments) {
            //得到二级
            List<Department> children = department.getChildren();
            //如果二级部门没有设置null
            if (children.size() == 0) {
                department.setChildren(null);
            }
            queryMulitParentDept(children);
        }
        return departments;
    }

    /**
     * 增加多级部门查询
     *
     * @param departmentList
     * @return
     */
    public void queryMulitParentDept(List<Department> departmentList) {
        if (departmentList.size() != 0) {
            for (Department department : departmentList) {
                //根据父id查询子部门
                List<Department> children = departmentMapper.queryDepByParentId(department.getId());
                //如果没有子部门设置null
                department.setChildren(children.size() == 0 ? null : children);
                //设置查询的子部门设置到上级
                department.setChildren(children);
                //再递归查询下级
                queryMulitParentDept(children);
            }
        }
    }

    /**
     * 修改多级部门
     *
     * @param departmentList
     * @param id
     */
    public void queryMulitParentDept(List<Department> departmentList, Long id) {
        if (departmentList.size() != 0) {
            for (Department department : departmentList) {
                //根据当前id查询到父id
                Long id2 = departmentMapper.queryParentId(id);
                Long id1 = department.getId();
                //判断选择到上级
                if (id2 != null) {
                    if (!id2.equals(id1)) {
                        //相同id
                        //根据父id查询子部门
                        List<Department> children = departmentMapper.queryDepByParentId(department.getId());
                        //如果没有子部门设置null
                        if (children.size() == 0) {
                            department.setChildren(null);
                        } else {
                            //设置查询的子部门设置到上级
                            department.setChildren(children);
                            //再递归查询下级
                            queryMulitParentDept(children, id);
                        }
                    } else {
                        department.setChildren(null);
                    }
                }
            }
        }
    }
}
